package com.blog.attach.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blog.attach.entity.Blog;
import com.blog.attach.entity.UserCollection;
import com.blog.attach.mapper.BlogMapper;
import com.blog.attach.mapper.UserCollectionMapper;
import com.blog.attach.service.UserCollectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 苏离
 * @since 2022-01-15
 */
@SuppressWarnings("all")
@Slf4j
@Service
@Transactional
public class UserCollectionServiceImpl extends ServiceImpl<UserCollectionMapper, UserCollection> implements UserCollectionService {
@Autowired
UserCollectionMapper collectionMapper;
@Autowired
    BlogMapper blogMapper;
    @Override
    public boolean isCollect(String userId, Integer articleId) {
        QueryWrapper<UserCollection> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("article_id", articleId);
        UserCollection userCollection = collectionMapper.selectOne(queryWrapper);
        if(ObjectUtils.isEmpty(userCollection))
        return false;
        return true;
    }

    @Override
    public boolean addOrDeleteCollect(boolean isAdd, String userId, Integer articleId) {
        if(isAdd){
            UserCollection collection=new UserCollection();
            collection.setUserId(userId);
            collection.setArticleId(articleId);
            int insert = collectionMapper.insert(collection);
            QueryWrapper<Blog> queryWrapper1=new QueryWrapper<>();
            queryWrapper1.eq("article_id", articleId);
            Blog blog = blogMapper.selectOne(queryWrapper1);
            blog.setBlogCollections(blog.getBlogCollections()+1);
            int i = blogMapper.updateById(blog);
            if(insert>0 && i >0)
                return true;
            return false;
        }
        else {
            QueryWrapper<UserCollection> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("user_id", userId).eq("article_id", articleId);
            int delete = collectionMapper.delete(queryWrapper2);

            QueryWrapper<Blog> queryWrapper3=new QueryWrapper<>();
            queryWrapper3.eq("article_id", articleId);
            Blog blog = blogMapper.selectOne(queryWrapper3);
            blog.setBlogCollections(blog.getBlogCollections()-1);
            int i = blogMapper.updateById(blog);
            if(delete>0 && i >0)
                return true;
            return false;
        }
    }

    @Override
    public Map<String, Object> getArticleCollection(String userId, Integer pageSize, Integer limit) {
        pageSize=(pageSize-1)*10;
        List<Blog> list=new ArrayList<>();
        List<Integer> articleIds = collectionMapper.getArticleIdByUserId(userId,pageSize,limit);
        articleIds.forEach(articleId ->{
            QueryWrapper<Blog> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("article_id", articleId);
            Blog blog = blogMapper.selectOne(queryWrapper);
            list.add(blog);
        });
        Map<String, Object> map=new HashMap<>();
        Integer articleCount = collectionMapper.getArticleCount(userId);
        map.put("total",articleCount);
        map.put("blog", list);
        return map;
    }


}
